package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {

    public boolean addTeacher(Teacher teacher) {
        TeacherDao teacherDao = new TeacherDao();
        boolean b = teacherDao.addTeacher(teacher);
        return b;
    }
    TeacherDao teacherDao = new TeacherDao();

    //判断输入的id在数组中对象的id是否一样
    public boolean isExist(String id) {
        //1.从到层里得到对象数组
        //TeacherDao teacherDao = new TeacherDao();
        Teacher[] arr = teacherDao.isExit();
        //2.遍历数组,首相对象数组中的对象不能为空
        //2.1假设id不存在
        boolean flage = false;
        for (int i = 0; i < arr.length; i++) {
            //确保数组元素都没有null
            if (arr[i] != null && arr[i].getId().equals(id)) {
                flage = true;
                break;
            }
        }
        return flage;
    }


    public Teacher[] findAllStudent() {
        //TeacherDao teacherDao = new TeacherDao();
        Teacher[] exit = teacherDao.isExit();
        //1.判断数组中有没有学生信息(有,返回地址值  没有,返回null)
        //思路:数组中只要存在一个数据,其他的可以都是null,就可以返回数组地址
        boolean fa = false;
        for (int i = 0; i < exit.length; i++){
            Teacher tea = exit[i];
            //确保数组元素都没有null元素
            if(tea != null){
                fa = true;
                break;
            }

        }
        //2.根据结果返回对应的数据
        if(fa){
            //有结果,返回地址
            return exit;
        }else{
            //没结果,返回null
            return null;
        }

    }
    public void deleteTeacherByid(String delId) {
        //将id传入到库管
        teacherDao.deleteTeacherByid(delId);
    }
    public void updateTeacher(String updateId, Teacher newTea) {
        //将修改的信息传到dao层进行处理
        teacherDao.updateTeacher(updateId,newTea);
    }
}
